<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    	<title>商品筛选器</title>
    <!--{include file='comm/comm_pic.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
</head>
<body>
<div style="width:100%">
    <div class="items clearfix"> 
        <h2 style="" class="tal">
        	正在操作：
        	<!--{if !isset($selectorInfo->selectID) || $selectorInfo->selectID == '' || $selectorInfo->selectID == null}-->添加<!--{else}-->修改<!--{/if}-->商品筛选器
        </h2>
        <div class="items2">
			<div>
                <form id="selector_form"  class="form-horizontal"  onsubmit="return false;">

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="selectName">
                            <span class="red">*</span>
                            筛选器名称：
                        </label>
                        <div class="col-sm-9">
                            <input id="selectName" class="form-control w300" type="text" name="selectName" value="<!--{$selectorInfo->selectName|default:''}-->" placeholder="请输入筛选器名称">
                            <input type="hidden" name="selectID" id="selectID" value="<!--{if isset($selectorInfo->selectID)}--><!--{$selectorInfo->selectID}--><!--{/if}-->">
                            <input type="hidden" name="dataCache" id="dataCache">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="bigCategoryID">
                            商品分类：
                        </label>
                        <div class="col-sm-9">
                            <select name="bigCategoryID" id="bigCategoryID" class="form-control w300" onchange="selector.getCategory(1);" style="width:125px; float:left;">
                                <option value="0">请选择</option>
                                <!--{foreach from=$bigArray item=big}-->
                                <option value="<!--{$big->bigCategoryID}-->"><!--{$big->description}--></option>
                                <!--{/foreach}-->
                            </select>
                            <select name="midCategoryID" id="midCategoryID" class="form-control w300" onchange="selector.getCategory(2);" style="width:125px; float:left;">
                                <option value="0">请选择</option>
                            </select>

                            <select name="smallCategoryID" id="smallCategoryID" class="form-control w300" style="width:125px; float:left;">
                                <option value="0">请选择</option>
                            </select>

                            <input type="button" name="categoryAdd" id="categoryAdd" onclick="selector.ruleAdd(1);" class="btn" value="添加" class="form-control w300" style="margin-left:18px; float:left;">
                        </div>
                    </div>



                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="brandID">
                            品牌：
                        </label>
                        <div class="col-sm-9">
                            <select name="brandID" id="brandID" class="form-control w300" style="width:125px; float:left;">
                                <option value="0">请选择</option>

                                <!--{foreach from=$goodsBrand item=brand}-->
                                <!--edit by wanglulu 20141203 start-->
                                <!--<option value="<!--{$brand->brandID}-->"><!--{$brand->brandName}--></option>-->
                                <option value="<!--{$brand->itemID}-->"><!--{$brand->itemName}--></option>
                                <!--edit by wanglulu 20141203 end-->
                                <!--{/foreach}-->
                            </select>

                            <input type="button" name="brandAdd" id="brandAdd" onclick="selector.ruleAdd(2);" class="btn" value="添加" style="margin-left:270px;">

                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="product_code">
                            商家编码：
                        </label>
                        <div class="col-sm-9">
                            <input type="text" name="product_code" id="product_code" class="form-control w300 fl" placeholder="请输入商家编码">
                            <input type="button" name="productAdd" id="productAdd" onclick="selector.ruleAdd(3);" class="btn" value="添加" style="margin-left:95px;">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="startPrice">
                            价格：
                        </label>
                        <div class="col-sm-9">
                            <input type="text" name="startPrice" id="startPrice" class="form-control w300" style="width:80px;float: left">
                            <span style="float: left;height: 36px;padding-top: 10px;">&nbsp;~&nbsp;</span>
                            <input type="text" name="endPrice" id="endPrice" class="form-control w300" style="width:80px;float: left">
                            <input type="button" name="priceAdd" id="priceAdd" onclick="selector.ruleAdd(4);" class="btn" value="添加" style="margin-left:216px;float: left">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="tagGroupID">
                            标签：
                        </label>
                        <div class="col-sm-9">
                            <select name="tagGroupID" id="tagGroupID" class="form-control w300" onchange="selector.getTag();" style="width:125px; float:left;">
                                <option value="0">请选择</option>
                                <!--{foreach from=$tagGroupInfo item=group}-->
                                <option value="<!--{$group->tagGroupID}-->"><!--{$group->tagGroupName}--></option>
                                <!--{/foreach}-->
                            </select>

                            <select name="tagID" id="tagID" class="form-control w300" style="width:125px; float:left;">
                                <option value="0">请选择</option>
                            </select>

                            <input type="button" name="tagAdd" id="tagAdd" onclick="selector.ruleAdd(5);" class="btn" value="添加" style="margin-left:145px;">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="searchKey">
                            关键字：
                        </label>
                        <div class="col-sm-9">
                            <input type="text" name="searchKey" id="searchKey" class="form-control w300 fl" placeholder="请输入关键字">
                            <input type="button" name="keywordAdd" id="keywordAdd" onclick="selector.ruleAdd(6);" class="btn" value="添加" style="margin-left:95px;float: left">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="selectorType">
                            使用规则：
                        </label>
                        <div class="col-sm-9">
                            <!--{if isset($selectorInfo)}-->
                            <!--{if $selectorInfo->selectorType == 1}-->
                            <input type="radio" class="square-radio" value="1" name="selectorType" checked /> 包含
                            <input type="radio" class="square-radio" value="2" name="selectorType" /> 排斥
                            <!--{else}-->
                            <input type="radio" class="square-radio" value="1" name="selectorType" /> 包含
                            <input type="radio" class="square-radio" value="2" name="selectorType" checked /> 排斥
                            <!--{/if}-->
                            <!--{else}-->
                            <input type="radio" class="square-radio" value="1" name="selectorType" checked /> 包含
                            <input type="radio" class="square-radio" value="2" name="selectorType" /> 排斥
                            <!--{/if}-->
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label" for="description">
                            描述：
                        </label>
                        <div class="col-sm-9">
                            <textarea style="height:80px;padding-left:10px;" name="description" id="description" class="form-control w500" placeholder="请输入促销描述"><!--{$selectorInfo->description|default:''}--></textarea>
                        </div>
                    </div>
		            
                    <div id="goodsSelector" style="width:900px;margin: 0 auto;">
						<table id="flex" style="width:100%;padding-bottom:0px;"></table>
					</div>

                </form>
            </div>
        </div>
    </div>

    <div class="bottom_fixbox">
        <div class="rectbox tac">
            <input type="button" class="btn" onclick="selector.check();" value="<!--{if !isset($selectorInfo->selectID) || $selectorInfo->selectID == '' || $selectorInfo->selectID == null}-->添加<!--{else}-->修改<!--{/if}-->筛选器" />&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn" onclick="E.closeWindows();" value="关闭" />
        </div>
    </div>


</div>
<script type="text/javascript">
	//表格对象
	var table_obj = {page : '', total : '', rows : []};
	
	$("#flex").flexigrid({
        url:"",
        dataType: "json",
        colModel : [
            {display: "选择器", width:400 , },
            {display: "类型", width:500 , },
        ],
        buttons : [
            {name: "删除", bclass: "delete", onpress : "selector.removeRow"}
        ],
        useRp: false,
        showTableToggleBtn: true
    });

    $('.square-radio').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%' // optional
    });

    var selector = {
        
    	i : 1,
    	
    	delete_str: '',
    	
        //检查数据
        check: function() {
            this.dt = E.getFormValues('selector_form');
            var error_msg = '';
            if (this.dt.selectName == '')
                error_msg += "筛选器名称不能为空<br />";
            
          	var ruleLength = $('[id*=row]').length
  			if(ruleLength == 0)
  				 error_msg += "请添加筛选规则<br />";
                
            if (error_msg != "")
                E.alert(error_msg);
            else
                E.confirm("您确认要<!--{if isset($selectorInfo->selectID) && $selectorInfo->selectID}-->修改<!--{else}-->添加<!--{/if}-->筛选器信息吗？", "selector.add");
        },

        //保存资源位信息
        add: function() {
        	var selectID = $.trim($('#selectID').val());
        	var category = $('#dataCache').data('category');
        	var selectorData = {dt : this.dt, rule : category};
            $.ajax({
                type: 'POST',
                url: '/invokingAjax-backend/promotion/goodsSelector.ajax?operFlg=7',
                dataType: "JSON",
                data: selectorData,
                success: function( result_obj ) {

                    if (result_obj.code == 200 && selectID != ''){
                        E.alert(result_obj.message, 2, 'E.closeWindows');
                        E.opener('selector.load');
                    }else if(result_obj.code == 200 && selectID == ''){
                    	 E.alert(result_obj.message, 2, 'E.refresh');  
                    	 E.opener('selector.load');
                    }	  
                    else
                        E.alert(result_obj.message);
                }
            });
        },
        
        //根据分类号获取中分类号或小分类号
        getCategory : function(flg){
        	if(flg == 1){	//大分类
        		var bigID = $('#bigCategoryID').val();
        		if(bigID == 0){
        			$('#midCategoryID').html('<option value="0">请选择</option>');
        			$('#smallCategoryID').html('<option value="0">请选择</option>');
        		}else{
        			$('#midCategoryID').html('<option value="0">请选择</option>');
        			$('#smallCategoryID').html('<option value="0">请选择</option>');
        			
        			//ajax查询中分类信息
        			var bigObj = {'flg' : 1, 'id' : bigID};
	        		$.ajax({
	        			url : "/invokingAjax-backend/promotion/goodsSelector.ajax?operFlg=4",
	        			type : 'POST',
	        			data : bigObj,
						success: function(result) {
							result = eval('(' + result + ')');
							
							if(result != 1){
								var htmlStr = '<option value="0">请选择</option>';
								$.each(result, function(k, v){
									htmlStr += '<option value=' + k + '>' + v + '</option>'
								})
								
								$('#midCategoryID').html(htmlStr);
							}else if(result == 1){
								$('#midCategoryID').html('<option value="0">请选择</option>');
        						$('#smallCategoryID').html('<option value="0">请选择</option>');
							}
						}
	        		});
        		}
        	}else if(flg == 2){	//中分类
        		var bigID = $('#bigCategoryID').val();
        		var midID = $('#midCategoryID').val();
        		if(midID == 0)
        			$('#smallCategoryID').html('<option value="0">请选择</option>');
        		else{
        			//ajax查询小分类信息
        			var midObj = {'flg' : 2, 'id' : bigID + '_' + midID};
        			$.ajax({
	        			url : "/invokingAjax-backend/promotion/goodsSelector.ajax?operFlg=4",
	        			type : 'POST',
	        			data : midObj,
						success: function(result) {
							result = eval('(' + result + ')');
							
							if(result != 1){
								var htmlStr = '<option value="0">请选择</option>';
								$.each(result, function(k, v){
									htmlStr += '<option value=' + k + '>' + v + '</option>'
								})
								
								$('#smallCategoryID').html(htmlStr);
							}else if(result == 1)
								$('#smallCategoryID').html('<option value="0">请选择</option>');
						}
	        		});
        		}
        	}
        },
        
        //根据标签组号获得标签信息
        getTag : function(){
        	var tagGroup = $('#tagGroupID').val();
        	if(tagGroup == 0)
        		$('#tagID').html('<option value="0">请选择</option>');
        	else{
        		$.ajax({
        			url : "/invokingAjax-backend/promotion/goodsSelector.ajax?operFlg=5&id="+tagGroup,
        			type : 'POST',
        			success : function(result){
        				result = eval('(' + result + ')');
        				
        				if(result == 1)
        					$('#tagID').html('<option value="0">请选择</option>');
        				else{
        					var htmlStr = '<option value="0">请选择</option>';
        					$.each(result, function(k, v){
        						htmlStr += '<option value=' + k + '>' + v + '</option>';
        					})
        					
        					$('#tagID').html(htmlStr);
        				}
        			}
        		});
        	}
        },
        
        //添加筛选器规则
        ruleAdd : function(flg){
        	
        	if(flg == 1){	//分类
        		
				var bigCategoryID   = $('#bigCategoryID').val(); 							//大分类号
				var bigCategoryText = $("#bigCategoryID").find('option:selected').text();	//大分类名称
				
				//判断中分类是否选择
				if($('#midCategoryID').val() != 0){
					var midCategoryID   = $('#midCategoryID').val(); 
					var midCategoryText = $('#midCategoryID').find('option:selected').text();
				}else{
					var midCategoryID   = 0;
					var midCategoryText = '';
				}
					
				//判断小分类是否选择
				if($('#smallCategoryID').val() != 0){
					var smallCategoryID   = $('#smallCategoryID').val(); 
					var smallCategoryText = $('#smallCategoryID').find('option:selected').text();
				}else{
					var smallCategoryID   = 0;
					var smallCategoryText = '';
				}
					
				if(!E.isInt(bigCategoryID) || parseInt(bigCategoryID) == 0){
					E.alert('请选择商品分类');
					return false;
				}
				
				//只存在中分类的情况下
				if(parseInt(midCategoryID) != 0 && parseInt(smallCategoryID) == 0){
					var bigID    = bigCategoryID;
					var midID    = bigCategoryID + midCategoryID;
					var midIDObj = flg + '_' + bigCategoryID + '-' + midCategoryID;
					var midText  = bigCategoryText + ' | ' + midCategoryText;
					
					//去除重复的分类号
					var categoryID_Obj = $('#dataCache').data('category');
					if(categoryID_Obj){
						var flg = 0;
						$.each(categoryID_Obj, function(k, v){
							if(midIDObj == k)
								flg = 1;
						});
						
						if(flg == 1){
							E.alert('此中分类已添加');
							return false;
						}	
					}else{
						//公共的Obj对象
						categoryID_Obj = {};
					}
					
					//判断是否存在其小分类
					var smallCategory_Obj = $('#dataCache').data('smallCategory');
					if(smallCategory_Obj){
						var num = 0
						$.each(smallCategory_Obj, function(k, v){
							var mid = v.substring(0, 5);
							if(midID == mid)
								num = 1;
						})
						
						if(num == 1){
							E.alert('此中分类已添加相关小分类');
							return false;
						}	
					}
					
					//判断是否存在此大分类，不存在可以添加相关的中分类，反之不能添加
					var bigCategory_Obj = $('#dataCache').data('bigCategory');
					if(bigCategory_Obj){
						var num = 0
						$.each(bigCategory_Obj, function(k, v){
							if(bigID == v)
								num = 1;
						})
						
						if(num == 1){
							E.alert('此中分类已添加相关大分类');
							return false;
						}	
					}else
						bigCategory_Obj = {};
					
					//大分类保存到bigCategory中
					bigCategory_Obj[bigID] = bigID;
					$("#dataCache").data('bigCategory', bigCategory_Obj);
					
					//中分类保存到midCategory分类当中
					var midCategory_Obj = $('#dataCache').data('midCategory');
					if(!midCategory_Obj)
						midCategory_Obj = {};
						
					midCategory_Obj[midID] = midID
					$("#dataCache").data('midCategory', midCategory_Obj);
					
					//保存到公共的Obj当中
					var midStr = bigCategoryID + ',' + midCategoryID
					categoryID_Obj[midIDObj] = [midStr, '分类' ,1];
					$('#dataCache').data('category', categoryID_Obj);
					
					//传入id和需要显示到页面的值
					var value = [midText, '分类'];
					this.tr_fun(midIDObj, value);
				}
				
				//大,中,小分类都存在情况下
				if(parseInt(midCategoryID) != 0 && parseInt(smallCategoryID) != 0){
					var bigID      = bigCategoryID;
					var midID      = bigCategoryID + midCategoryID;
					var smallID    = bigCategoryID + midCategoryID + smallCategoryID;
					var smallIDObj = flg + '_' + bigCategoryID + '-' + midCategoryID + '-' + smallCategoryID;
					var smallText  = bigCategoryText + ' | ' + midCategoryText + ' | ' + smallCategoryText;
					
					//检查是否有相同的小分类
					var categoryID_Obj = $('#dataCache').data('category');
					if(categoryID_Obj){
						var flg = 0;
						$.each(categoryID_Obj, function(k, v){
							if(smallIDObj == k)
								flg = 1;
						});
						
						if(flg == 1){
							E.alert('此小分类已添加');
							return false;
						}	

					}else
						categoryID_Obj = {};
					
					//判断小分类上一级大分类是否已添加
					var bigCategory_Obj = $('#dataCache').data('bigCategory');
					if(bigCategory_Obj){
						var num = 0
						$.each(bigCategory_Obj, function(k, v){
							if(bigID == v)
								num = 1;
						})
						
						if(num == 1){
							E.alert('此小分类已添加相关大分类');
							return false;
						}	
					}else
						bigCategory_Obj = {};
					
					//判断是否存在其中分类
					var midCategory_Obj = $('#dataCache').data('midCategory');
					if(midCategory_Obj){
						var num = 0
						$.each(midCategory_Obj, function(k, v){
							if(midID == v)
								num = 1;
						});
						
						if(num == 1){
							E.alert('此小分类已添加相关中分类');
							return false;
						}	
					}
					
					//大分类保存到bigCategory中
					bigCategory_Obj[bigID] = bigID;
					$("#dataCache").data('bigCategory', bigCategory_Obj);

					//保存小分类到smallCategory中
					var smallCategory_Obj = $('#dataCache').data('smallCategory');
					if(!smallCategory_Obj)
						smallCategory_Obj = {};
						
					smallCategory_Obj[smallID] = smallID;
					$('#dataCache').data('smallCategory', smallCategory_Obj);
			
					//保存到公共的Obj当中
					var smallStr = bigCategoryID + ',' + midCategoryID + ',' + smallCategoryID;
					categoryID_Obj[smallIDObj] = [smallStr, '分类', 1];
					$('#dataCache').data('category', categoryID_Obj);
					
					var value = [smallText, '分类'];
					this.tr_fun(smallIDObj, value);
				}
				
				//只存在大分类情况
				if(parseInt(midCategoryID) == 0 && parseInt(smallCategoryID) == 0){
					var bigID      = bigCategoryID;
					var bigIDObj   = flg + '_' + bigCategoryID;
					var bigText    = bigCategoryText;
					
					//判断是否有相同的大分类
					var categoryID_Obj = $('#dataCache').data('category');
					if(categoryID_Obj){
						var flg = 0;
						$.each(categoryID_Obj, function(k, v){
							if(bigIDObj == k)
								flg = 1;
						})
						
						if(flg == 1){
							E.alert('此大分类已添加');
							return false;
						}	
							
					}else{
						//用来保存公共的分类.
						categoryID_Obj = {};
					}
					
					//判断是否存在中分类
					var midCategory_Obj = $('#dataCache').data('midCategory');
					if(midCategory_Obj){
						var flg = 0;
						$.each(midCategory_Obj, function(k, v){
							var big = v.substring(0, 2);
							if(bigID == big)
								flg = 1;
						})
						
						if(flg == 1){
							E.alert('此大分类已添加相关中分类');
							return false;
						}	
					}
					
					//判断是否存在其小分类
					var smallCategory_Obj = $('#dataCache').data('smallCategory');
					if(smallCategory_Obj){
						var num = 0
						$.each(smallCategory_Obj, function(k,v){
							var big = v.substring(0, 2);
							if(bigID == big)
								num = 1;
						})
						
						if(num == 1){
							E.alert('此大分类已添加相关小分类');
							return false;
						}	
					}
					
					//保存大分类到bigCategory中
					var bigCategory_Obj = $("#dataCache").data("bigCategory");
					if(!bigCategory_Obj)
						bigCategory_Obj = {};
			
					bigCategory_Obj[bigID] = bigID;
					$('#dataCache').data('bigCategory', bigCategory_Obj);
					
					//保存到公共的Obj当中
					categoryID_Obj[bigIDObj] = [bigID, '分类', 1];
					$('#dataCache').data('category', categoryID_Obj);
					
					var value = [bigText, '分类'];
					this.tr_fun(bigIDObj, value);
				}
        	}else if(flg == 2){	//品牌
        		var brandID     = $("#brandID").val();
        		var brandIDObj  = flg + '_' + $("#brandID").val();
				var brandText   = $("#brandID").find("option:selected").text();
				if(parseInt(brandID) == 0){
					E.alert('请选择品牌');
					return false;
				}
				
				var categoryID_Obj = $("#dataCache").data("category");
				if(categoryID_Obj){
					var flg = 0;
					$.each(categoryID_Obj, function(k, v){
						if(brandIDObj == k)
							flg = 1;
					})
					
					if(flg == 1){
						E.alert('此品牌已添加');
						return false;
					}
				}else{
					//公共的Obj对象
					categoryID_Obj = {};
				}
				
				categoryID_Obj[brandIDObj] = [brandID, '品牌' ,4];
				$('#dataCache').data('category', categoryID_Obj);
				
				var value = [brandText, '品牌'];
				this.tr_fun(brandIDObj, value);
        	}else if(flg == 3){	//商品编码
        		//var product_code = trim($('#product_code').val());
                var product_code = $('#product_code').val().trim();
                var productObj = flg + '_' + product_code;
				if(!product_code){
					E.alert('请输入商家编码');
					return false;
				}
				
				var categoryID_Obj = $('#dataCache').data('category');
				if(categoryID_Obj){
					var flg = 0
					$.each(categoryID_Obj, function(k, v){
                        //mod by 20141113 wanglulu   商品SKU兼容大小写  --start
						if(productObj.toLocaleLowerCase() == k || productObj.toLocaleUpperCase() == k)
                        //mod by 20141113 wanglulu   商品SKU兼容大小写  --end
							flg = 1
					});
					
					if(flg == 1){
						E.alert('此商家编码已添加');
						return false;
					}
				}else
					categoryID_Obj = {};
				
				//ajax查询商品信息
				var product = {'product_code' : product_code};
				$.ajax({
					url : '/invokingAjax-backend/promotion/goodsSelector.ajax?operFlg=6',
					type : 'POST',
					data : product,
					success : function(result){
						result = eval('(' + result + ')');
						
						if(result.code == 200){
							var code = result.data.product_code;
							categoryID_Obj[productObj] = [code, '商家编码', 2];
							$('#dataCache').data('category', categoryID_Obj);
							
							var value = [code, '商家编码'];
							selector.tr_fun(productObj, value);
							$('#product_code').val('');
						}else{
							E.alert(result.message);
							$('#product_code').val('');
						}	
					}
				})		
					
        	}else if(flg == 4){	//价格
        		var startPrice = $('#startPrice').val().trim();
				var endPrice   = $('#endPrice').val().trim();
				var priceObj   = flg + '_' + startPrice + endPrice;
				if(!E.isNum(startPrice) || !E.isNum(endPrice)){
					E.alert('商品销售价格必须是数字');
					return false;
				}

				if(parseInt(startPrice) > parseInt(endPrice)){
					E.alert('商品销售起始价格必须小于结束价格');
					return false;
				}
				
				var marketPrice = startPrice + ',' + endPrice
				var categoryID_Obj = $('#dataCache').data('category');
				if(categoryID_Obj){
					var flg = 0;
					$.each(categoryID_Obj, function(k, v){
						if(priceObj == k){
							flg = 1;
						}
					})
					
					if(flg == 1){
						E.alert('此价格范围已添加');
						return false;
					}
				}else
					categoryID_Obj = {};
				
				categoryID_Obj[priceObj] = [marketPrice, '价格范围', 3];
				$('#dataCache').data('category', categoryID_Obj);
				var value = [marketPrice, '价格范围'];
				this.tr_fun(priceObj, value);
				$('#startPrice').val('');
				$('#endPrice').val('');
				
        	}else if(flg == 5){	//标签
        		var tagGroupID	 = $("#tagGroupID").val();
				var tagGroupText = $("#tagGroupID").find("option:selected").text();
				var tagID		 = $("#tagID").val();
				var tagText      = $("#tagID").find("option:selected").text();
				
				if(tagText == '请选择')
					tagText = '';
				else
					tagText = ' | ' + tagText;
				
				if(parseInt(tagID) == 0){
					var tag      = tagGroupID;
					var tagIDObj = flg + '_' + tagGroupID;
					var tagStr   = tagGroupID;
				}else{
					var tag      = tagGroupID + tagID;
					var tagIDObj = flg + '_' + tagGroupID + '-' + tagID;;
					var tagStr   = tagGroupID + ',' + tagID;
				}
				
				var tag_text = tagGroupText + tagText;
						
				if(parseInt(tagGroupID) == 0){
					E.alert('请选择标签组');
					return false;
				}
				
				//添加标签组
				if(parseInt(tagGroupID) != 0 && parseInt(tagID) == 0){
					//判断是否存在子标签
					var tag_obj = $('#dataCache').data('tag_obj');
					if(tag_obj){
						var num = 0;
						$.each(tag_obj, function(k, v){
							if(tagGroupID == v)
								num = 1;
						});
						if(num == 1){
							E.alert('此标签组已添加相关子标签');
							return false;
						}
					}
					
					//保存标签组
					var tagGroup_obj = $('#dataCache').data('tagGroupObject');
					if(!tagGroup_obj)
						tagGroup_obj = {};
					
					tagGroup_obj[tagGroupID] = tagGroupID
					$('#dataCache').data('tagGroupObject',tagGroup_obj);	
				}
				
				//添加是的标签组+标签
				if(parseInt(tagGroupID) != 0 && parseInt(tagID) != 0){
					var tagGroup_obj = $('#dataCache').data('tagGroupObject');
					if(tagGroup_obj){
						var num = 0;
						$.each(tagGroup_obj, function(k, v){
							if(tagGroupID == v)
								num = 1
						})
						if(num == 1){
							E.alert('此标签已添加上级标签组');
							return false;
						}
					}
					
					//保存标签组
					var tagGroup_obj = $('#dataCache').data('tagGroupObject');
					if(!tagGroup_obj)
						tagGroup_obj = {};
					
					tagGroup_obj[tagGroupID] = tagGroupID
					$('#dataCache').data('tagGroupObject', tagGroup_obj);
					
					//保存标签组+标签
					var tag_obj = $('#dataCache').data('tag_obj');
					if(!tag_obj)
						tag_obj = {};
					
					tag_obj[tag] = tagGroupID
					$('#dataCache').data('tag_obj', tag_obj);	
				}
				
				var categoryID_Obj = $('#dataCache').data('category');
				if(categoryID_Obj){
					var flg = 0;
					$.each(categoryID_Obj, function(k, v){
						if(tagIDObj == k)
							flg = 1;
						
					})
					if(flg == 1){
						E.alert('此标签已添加');
						return false;
					}
				}else
					categoryID_Obj = {};
				
				categoryID_Obj[tagIDObj] = [tagStr, '标签', 5];
				$('#dataCache').data('category', categoryID_Obj);
				var value = [tag_text, '标签'];
				this.tr_fun(tagIDObj, value);
				
        	}else if(flg == 6){	//关键字
        		var searchKey = $.trim($('#searchKey').val());
				if(!searchKey){
					E.alert('请填写关键字');
					return false;
				}
				
				var categoryID_Obj = $('#dataCache').data('category');
				if(categoryID_Obj){
					var flg = 0;
					$.each(categoryID_Obj, function(k, v){
						if(searchKey == v[0])
							flg = 1;
					})
					
					if(flg == 1){
						E.alert('此关键字已添加');
						return false;
					}
				}else{
					//公共的Obj对象
					categoryID_Obj = {};
				}
				
				categoryID_Obj['k'+this.i] = [searchKey, '关键字', 6];
				$('#dataCache').data('category', categoryID_Obj);
				var value = [searchKey, '关键字'];
				this.tr_fun('k'+this.i, value);
				$('#search_key').val('');
				this.i++;
        	}
        },
        
        //添加一行
		 tr_fun : function(id, value){

			table_obj.rows.push({'id' : id, 'cell' : value});
        	
        	$("#flex").flexAddData(table_obj);	
			
			var cache = $('#dataCache').data('category');
			console.log(cache);
        },
        
        //删除选中的行
        removeRow : function(){
        	this.delete_str = '';
            $.each($('#flex').find('tr'), function(k, v) {
                if ($(v).hasClass('trSelected')) {
                	if($(v).attr('id') != undefined)
                    	selector.delete_str += '|' + $(v).attr('id').replace('row', '');
                }
            });

            if (this.delete_str == '')
                E.alert('请选择需要删除的选择器');
            else
                E.confirm('您确认要删除选中的选择器吗？', 'selector.deleteConfirm');
        },
        
        //删除筛选器执行函数
        deleteConfirm : function(){
			this.delete_str = this.delete_str.substr(1);
			var delete_row_array = this.delete_str.split('|');
			
			//循环删除dataCache中的数据	
			$.each(delete_row_array, function(k, v){
				var dataArray = v.split('_');
				if(dataArray[0] == 1){	//分类
					var categoryArray = dataArray[1].split('-');
					
					if(categoryArray.length == 1){	//大分类
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(bigCategory_Obj){
							$.each(bigCategory_Obj, function(k, v){
								if(categoryArray[0] == k){
									delete bigCategory_Obj[k];
								}
							})
						}
					}else if(categoryArray.length == 2){ //中分类
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(bigCategory_Obj){
							$.each(bigCategory_Obj, function(k, v){
								if(categoryArray[0] == k){
									delete bigCategory_Obj[k];
								}
							})
						}
						
						var midCategory_Obj = $('#dataCache').data('midCategory');
						if(midCategory_Obj){
							$.each(midCategory_Obj,function(k,v){
								if(categoryArray[0]+categoryArray[1] == k){
									delete midCategory_Obj[k];
								}
							})
						}
						
					}else if(categoryArray.length == 3){ //小分类
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(bigCategory_Obj){
							$.each(bigCategory_Obj, function(k, v){
								if(categoryArray[0] == k){
									delete bigCategory_Obj[k];
								}
							})
						}
						
						var midCategory_Obj = $('#dataCache').data('midCategory');
						if(midCategory_Obj){
							$.each(midCategory_Obj,function(k,v){
								if(categoryArray[0]+categoryArray[1] == k){
									delete midCategory_Obj[k];
								}
							})
						}
						
						var smallCategory_Obj = $("#dataCache").data("smallCategory");
						if(smallCategory_Obj){
							$.each(smallCategory_Obj,function(k,v){
								if(categoryArray[0]+categoryArray[1]+categoryArray[2] == k){
									delete smallCategory_Obj[k];
								}
							})
						}
					}
				}else if(dataArray[0] == 5){	//标签
					var tagArray = dataArray[1].split('-');
					if(tagArray.length == 1){	//标签组
						var tagGroup_obj = $('#dataCache').data('tagGroupObject');
						if(tagGroup_obj){
							$.each(tagGroup_obj, function(k, v){
								if(tagArray[0] == k){
									delete tagGroup_obj[k];
								}
							})
						}
					}else if(tagArray.length == 2){	//标签
						var tagGroup_obj = $('#dataCache').data('tagGroupObject');
						if(tagGroup_obj){
							$.each(tagGroup_obj, function(k, v){
								if(tagArray[0] == k){
									delete tagGroup_obj[k];
								}
							})
						}
						
						var tag_obj = $('#dataCache').data('tag_obj');
						if(tag_obj){
							$.each(tag_obj, function(k, v){
								if(tagArray[0]+tagArray[1] == k){
									delete tag_obj[k];
								}
							})
						}
					}
				}
				
				//删除dataCache中的category数据信息 
				var categoryObj = $('#dataCache').data('category');
				if(categoryObj){
					$.each(categoryObj, function(ck, cv){
						if(v == ck)
							delete categoryObj[ck];
					})
				}
				
				//删除行对象数据
				$.each(table_obj.rows, function(rk, rv){
					if(rv != undefined && rv.id == v){
						
						table_obj.rows.splice(rk, 1);
					}	
				})		
			})
			
			//删除选中的行
			$('#flex tr').each(function(k, v){
				if($(this).hasClass('trSelected')){
					$(this).remove();
				}
			})
        }
    };

    //编辑初始化显示选择器
    $(document).ready(function() {
		<!--{if isset($selectorRule)}-->
			<!--{foreach from=$selectorRule item=rule}-->
				//保存数据到dataCache中
				var selectType = <!--{$rule->selectType}-->;
				if(selectType == 1){	//分类
					var selector_str = '<!--{$rule->selector}-->';
					var cateArr = selector_str.split(',');
					if(cateArr.length == 1){ //大分类
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(!bigCategory_Obj)
							bigCategory_Obj = {};
							
						//大分类保存到bigCategory中	
						bigCategory_Obj[cateArr[0]] = cateArr[0];
						$('#dataCache').data('bigCategory', bigCategory_Obj);
							
					}else if(cateArr.length == 2){ //中分类
						
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(!bigCategory_Obj)
							bigCategory_Obj = {};
							
						//大分类保存到bigCategory中	
						bigCategory_Obj[cateArr[0]] = cateArr[0];
						$('#dataCache').data('bigCategory', bigCategory_Obj);
						
						var midCategory_Obj = $('#dataCache').data('midCategory');
						if(!midCategory_Obj)
							midCategory_Obj = {};
						
						var midID = cateArr[0]+cateArr[1];		
						midCategory_Obj[midID] = midID
						$('#dataCache').data('midCategory', midCategory_Obj);
						
					}else if(cateArr.length == 3){ //小分类
						var bigCategory_Obj = $('#dataCache').data('bigCategory');
						if(!bigCategory_Obj)
							bigCategory_Obj = {};
							
						//大分类保存到bigCategory中	
						bigCategory_Obj[cateArr[0]] = cateArr[0];
						$('#dataCache').data('bigCategory', bigCategory_Obj);
						
						var smallCategory_Obj = $('#dataCache').data('smallCategory');
						if(!smallCategory_Obj)
							smallCategory_Obj = {};
							
						var smallID = cateArr[0] + cateArr[1] + cateArr[2];
						smallCategory_Obj[smallID] = smallID;
						$("#dataCache").data('smallCategory', smallCategory_Obj);
					}
				}else if(selectType == 5){ //标签
					var selector_str = '<!--{$rule->selector}-->';
					var tagArr = selector_str.split(',');
					if(tagArr.length == 1){	//标签组
						var tagGroup_obj = $('#dataCache').data('tagGroupObject');
						if(!tagGroup_obj)
							tagGroup_obj = {};
						
						tagGroup_obj[tagArr[0]] = tagArr[0];
						$('#dataCache').data('tagGroupObject', tagGroup_obj);
						
					}else if(tagArr.length == 2){	//标签
						var tagGroup_obj = $('#dataCache').data('tagGroupObject');
						if(!tagGroup_obj)
							tagGroup_obj = {};
						
						tagGroup_obj[tagArr[0]] = tagArr[0];
						$('#dataCache').data('tagGroupObject', tagGroup_obj);
						
						//保存标签组+标签
						var tag_obj = $('#dataCache').data('tag_obj');
						if(!tag_obj)
							tag_obj = {};
						
						var tag = tagArr[0] + tagArr[1]
						tag_obj[tag] = tagArr[0];
						$('#dataCache').data('tag_obj', tag_obj);	
					}
				}
				
				//保存到dataCache category中
				var categoryID_Obj = $('#dataCache').data('category');
				if(!categoryID_Obj)
					categoryID_Obj = {};
					
				categoryID_Obj['<!--{$rule->ruleID}-->'] = ['<!--{$rule->selector}-->', '<!--{$rule->ruleType}-->', selectType];
				$('#dataCache').data('category', categoryID_Obj);
			
	        	var ruleID    = '<!--{$rule->ruleID}-->';
	        	var val = ['<!--{$rule->ruleStr}-->', '<!--{$rule->ruleType}-->'];
	        	
				selector.tr_fun(ruleID, val);
			<!--{/foreach}-->
		<!--{/if}-->
    });
</script>

</body>

</html>
